<?php
	require_once 'login.php';

	if(isset($_POST['cancella'])) {

		foreach($_POST as $key => $val) {
			$key = cleanNumber($key,0);
			if(($key != 'cancella') && $val == 'on') {
				$qry = 'DELETE FROM SMS WHERE Id=\''.$key.'\'';
				$check = mysql_query($qry);
				
			}
		}
	}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
    <?php
            include_once('generico.php');
            include_once('funzioni.php');
    
	generaHead();
	?>
    <body>
        <div id="container">
        <?php
            generaHeader();
            generaMenusx();
            generaMenuLogin();

            echo '
                <div id="content">';

   if(isset($_SESSION['logged']) && $_SESSION['logged']) {	
                echo '<h2 class="titolo">Posta</h2>';

		$qry = "SELECT COUNT(Id) as Arrivo FROM SMS WHERE A='".$_SESSION['Utente']."' AND SalvaInUscita=0";
		$check = mysql_query($qry);
		$array = mysql_fetch_assoc($check);
		$arrivo=$array['Arrivo'];
		echo '<span class="tab"><a href="posta.php?p=1">In Arrivo('.$array['Arrivo'].')</a></span>';

		$qry = "SELECT COUNT(Id) as Uscita FROM SMS WHERE DA = '".$_SESSION['Utente']."' AND SalvaInUscita=1 ";
		$check = mysql_query($qry);
		$array = mysql_fetch_assoc($check);
		echo '<span class="tab"><a href="posta.php?p=2">In Uscita('.$array['Uscita'].')</a></span>';
		$uscita=$array['Uscita'];
		$total=$arrivo+$uscita;
		echo '<span class="tab"><a href="posta.php?p=3">Tutti('.$total.')</a></span>';
		echo '<div id="pannello">';

		if(isset($_GET['o'])) $o = cleanSimpleString($_GET['o']);
		if(isset($o)){
			$order = " ORDER BY $o";
			
			if (isset($_GET['u'])) $u=cleanNumber($_GET['u']);
			if (isset($u) && !$u){
				$order .=" ASC";
			}
			else $order .=" DESC";
		}
		else $order = " ORDER BY Data DESC";

		$p = (isset($_GET['p'])) ? cleanNumber($_GET['p']) : 1;
		if(isset($_GET['Id'])) {
					$id = cleanNumber($_GET['Id']);
					$user = $_SESSION['Utente'];
	                $qry = "SELECT Da as Mittente, A as Destinatario, Corpo, Data, Titolo FROM SMS
        	                        WHERE Id = '$id' AND ((A='$user' AND SalvaInUscita!=1)OR (Da='$user' AND SalvaInUscita=1))";
        	        $check = mysql_query($qry);
			$array = mysql_fetch_assoc($check);
			echo '<h2 class="titolo">'.cleanSimpleString($array['Titolo']).'</h2>
					<h3>Mittente: '.cleanSimpleString($array['Mittente']).'</h3>
					<h3>Destinatario: '.cleanSimpleString($array['Destinatario']).'</h3>
					<p>'.stripslashes($array['Corpo']).'</p>
					<p>'.htmlentities($array['Data']).'</p>
					<a href="messaggio.php?to='.$array['Mittente'].'&amp;t='.$array['Titolo'].'"><input type="button"  value="Rispondi"></a>
					
					'
					;
					
		}
		else if($p==1) { 
			$numero = $arrivo;
			if(isset($_GET['pag']))
				$pag = cleanNumber($_GET['pag']);
			else 
				$pag = ((int)(($numero-1)/10));
					
	                $qry = "SELECT Id, Da as Mittente, Data, Titolo FROM SMS
        	                        WHERE A = '".$_SESSION['Utente']."' AND SalvaInUscita=0 ".$order." LIMIT ".($pag*10).", 10 ";
        	        $check = mysql_query($qry);
  
        	        echo '	
        	                <p>Messaggi in arrivo</p>
        	                <form name="conferma" method="post" action="'.$_SERVER['PHP_SELF?p=1'].'">';

			generaTabellaMessaggi($check, $array, $pag);

			echo '<input type="submit" value="Cancella" name="cancella">
				</form>';
        	}
			
		else if($p==2) { 
			$numero = $uscita;
			if(isset($_GET['pag']))
				$pag = cleanNumber($_GET['pag']);
			else 
				$pag = ((int)(($numero-1)/10));

	                
	                $qry = "SELECT Id, Da as Mittente, Data, Titolo FROM SMS
        	                        WHERE DA = '".$_SESSION['Utente']."' AND SalvaInUscita=1  ".$order." LIMIT ".($pag*10).", 10 ";
        	        $check = mysql_query($qry);
  
        	        echo '	
        	                <p>Messaggi in uscita</p>
        	                <form name="conferma" method="post" action="'.$_SERVER['PHP_SELF?p=1'].'">';

			generaTabellaMessaggi($check, $array, $pag);

			echo '<input type="submit" value="Cancella" name="cancella">
				</form>';
		}
				
		else if($p==3) { 
			$numero = $total;
			if(isset($_GET['pag']))
				$pag = cleanNumber($_GET['pag']);
			else 
				$pag = ((int)(($numero-1)/10));
					
	                $qry = "SELECT Id,Da as Mittente, A as Destinatario, Data, Titolo FROM SMS
        	                        WHERE  (A='".$_SESSION['Utente']."' AND SalvaInUscita=0) OR (Da='".$_SESSION['Utente']."' AND SalvaInUscita=1)  ".$order." LIMIT ".($pag*10).", 10 ";
        	        $check = mysql_query($qry);

        	        echo '<p>Messaggin in arrivo e in uscita</p>
        	                <form name="total" method="post" action="'.$_SERVER['PHP_SELF?p=3'].'">';

			generaTabellaMessaggi($check, $array, $pag);

			echo '<input type="submit" value="Cancella" name="cancella"></form>';
		}
			
		
		echo '</div>';

		//Frecce di navigazione pagine
		echo '<p class="navPagine">';

		if($pag > 0)
			echo '<a href="posta.php?p='.$p.'&amp;pag='.($pag-1).'" title="Pagina precedente">&lt;&lt; indietro</a>';
		else
			echo '<span class="gray">&lt;&lt; indietro</span>';
		echo '&emsp;';
		if($pag!=0) 
			echo '<a href="posta.php?p='.$p.'&amp;pag=0" title="Prima pagina">1</a>';
		else
			echo '<span class="gray" title="Pagina successiva">1</span>';

		echo ' . . . ';
		echo '<span title="Pagina corrente">'.($pag+1).'</span>';
		echo ' . . . ';

		if((($pag+1)*10) < $numero) {
			echo '<a href="posta.php?p='.$p.'&amp;pag='.((int)(($numero-1)/10)).'" title="Ultima pagina">'.((int)(($numero-1)/10)+1).'</a>';					
			echo '&emsp;';
			echo '<a href="posta.php?p='.$p.'&amp;pag='.($pag+1).'" title="Pagina successiva">avanti &gt;&gt;</a>';
		}
		else {
			echo '<span class="gray" title="Ultima pagina">'.((int)(($numero-1)/10)+1).'</span>';
			echo '&emsp;';	
			echo '<span class="gray" title="Pagina successiva">avanti &gt;&gt;</span>';
		}


	    }
            else
                echo '<p>Devi effettuare il login per accedere alla posta</p>';
	
            echo '</div>';

            generaFooter();
        ?>
        </div>
    </body>
</html>

